/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.models.document;

import java.util.HashMap;
import java.util.Map;
import org.brainstretch.models.quizzes.Question;
import org.brainstretch.models.quizzes.QuestionSet;
import org.brainstretch.models.quizzes.Quiz;

/**
 *
 * @author mrodaitis
 */
public class Tokenizer {
    
    public static Map<String, Posting> getAllPostings(Quiz quiz){
        Map<String, Posting> postingMap = new HashMap<String, Posting>();
        
        //Parse the title
        String[] titleParts = quiz.getTitle().split("\\s+");
        for(String part: titleParts){
            addTerm(part, postingMap, quiz);
        }
        
        for(QuestionSet qSet: quiz.getQuestionSets()){
            for(Question question: qSet.getQuestions()){
                String[] textTerms = question.getTextClue().getText().split("\\s+");
                for(String term: textTerms){
                    addTerm(term, postingMap, quiz);
                }
                String[] solutionTerms = question.getSolution().getText().split("\\s+");
                for(String term: solutionTerms){
                    addTerm(term, postingMap, quiz);
                }
                
            }
        }
        
        return postingMap;
    }
    
    public static void addTerm(String term, Map<String, Posting> map, Quiz quiz){
        String lowerTerm = term.toLowerCase();
        Posting posting = map.get(lowerTerm);
        if(posting == null){
            posting = new Posting();
            posting.setQuiz(quiz);
            map.put(lowerTerm, posting);
        }
        else{
            posting.incrementFrequency();
        }
        
    }
    
}
